package c.c.b.a.a;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.TimeUnit;

/* compiled from: AsyncTimeout */
public class c extends x {
    public static final long h;
    public static final long i;
    public static c j;
    public boolean e;
    public c f;
    public long g;

    /* compiled from: AsyncTimeout */
    public static final class a extends Thread {
        public a() {
            super("Okio Watchdog");
            setDaemon(true);
        }

        /* JADX WARNING: Removed duplicated region for block: B:0:0x0000 A:{LOOP_START, SYNTHETIC, LOOP:0: B:0:0x0000->B:22:0x0000, Splitter:B:0:0x0000} */
        /* JADX WARNING: Missing exception handler attribute for start block: B:0:0x0000 */
        /* JADX WARNING: Can't wrap try/catch for region: R(3:0|1|2) */
        /* JADX WARNING: Missing block: B:15:?, code skipped:
            r1.j();
     */
        public void run() {
            /*
            r3 = this;
        L_0x0000:
            r0 = c.c.b.a.a.c.class;
            monitor-enter(r0);	 Catch:{ InterruptedException -> 0x0000 }
            r1 = c.c.b.a.a.c.m();	 Catch:{ all -> 0x0019 }
            if (r1 != 0) goto L_0x000b;
        L_0x0009:
            monitor-exit(r0);	 Catch:{ all -> 0x0019 }
            goto L_0x0000;
        L_0x000b:
            r2 = c.c.b.a.a.c.j;	 Catch:{ all -> 0x0019 }
            if (r1 != r2) goto L_0x0014;
        L_0x000f:
            r1 = 0;
            c.c.b.a.a.c.j = r1;	 Catch:{ all -> 0x0019 }
            monitor-exit(r0);	 Catch:{ all -> 0x0019 }
            return;
        L_0x0014:
            monitor-exit(r0);	 Catch:{ all -> 0x0019 }
            r1.j();	 Catch:{ InterruptedException -> 0x0000 }
            goto L_0x0000;
        L_0x0019:
            r1 = move-exception;
            monitor-exit(r0);	 Catch:{ all -> 0x0019 }
            throw r1;	 Catch:{ InterruptedException -> 0x0000 }
            */
            throw new UnsupportedOperationException("Method not decompiled: c.c.b.a.a.c$a.run():void");
        }
    }

    static {
        long toMillis = TimeUnit.SECONDS.toMillis(60);
        h = toMillis;
        i = TimeUnit.MILLISECONDS.toNanos(toMillis);
    }

    public static c m() throws InterruptedException {
        Class cls = c.class;
        c cVar = j.f;
        c cVar2 = null;
        if (cVar == null) {
            long nanoTime = System.nanoTime();
            cls.wait(h);
            if (j.f == null && System.nanoTime() - nanoTime >= i) {
                cVar2 = j;
            }
            return cVar2;
        }
        long nanoTime2 = cVar.g - System.nanoTime();
        if (nanoTime2 > 0) {
            long j = nanoTime2 / 1000000;
            cls.wait(j, (int) (nanoTime2 - (1000000 * j)));
            return null;
        }
        j.f = cVar.f;
        cVar.f = null;
        return cVar;
    }

    public final void h() {
        if (this.e) {
            throw new IllegalStateException("Unbalanced enter/exit");
        }
        long j = this.c;
        boolean z = this.a;
        int i = (j > 0 ? 1 : (j == 0 ? 0 : -1));
        if (i != 0 || z) {
            this.e = true;
            Class cls = c.class;
            synchronized (cls) {
                c cVar;
                if (j == null) {
                    j = new c();
                    a aVar = new a();
                    aVar.setName("tt_pangle_thread_watch_dog");
                    aVar.start();
                }
                long nanoTime = System.nanoTime();
                if (i != 0 && z) {
                    this.g = Math.min(j, d() - nanoTime) + nanoTime;
                } else if (i != 0) {
                    this.g = j + nanoTime;
                } else if (z) {
                    this.g = d();
                } else {
                    throw new AssertionError();
                }
                j = this.g - nanoTime;
                c cVar2 = j;
                while (true) {
                    cVar = cVar2.f;
                    if (cVar == null) {
                        break;
                    } else if (j < cVar.g - nanoTime) {
                        break;
                    } else {
                        cVar2 = cVar;
                    }
                }
                this.f = cVar;
                cVar2.f = this;
                if (cVar2 == j) {
                    cls.notify();
                }
            }
        }
    }

    public final void i(boolean z) throws IOException {
        if (l() && z) {
            throw k(null);
        }
    }

    public void j() {
    }

    public IOException k(IOException iOException) {
        InterruptedIOException interruptedIOException = new InterruptedIOException("timeout");
        if (iOException != null) {
            interruptedIOException.initCause(iOException);
        }
        return interruptedIOException;
    }

    public final boolean l() {
        boolean z = false;
        if (!this.e) {
            return false;
        }
        this.e = false;
        synchronized (c.class) {
            c cVar = j;
            while (cVar != null) {
                c cVar2 = cVar.f;
                if (cVar2 == this) {
                    cVar.f = this.f;
                    this.f = null;
                    break;
                }
                cVar = cVar2;
            }
            z = true;
        }
        return z;
    }
}
